package com.lfy.modules.common.custom.constants;

import java.time.LocalDate;

public class FileConstant {
    public static final long MAX_IMG_FILE_SIZE = 1024 * 1024 *10;
    public static final long MAX_DISK_FILE_SIZE = 1024 * 1024 *6;


    public static final String IMG_FILE_PREFIX = "image/";
    public static final String COVER_DIR = "cover/";
    public static final String ARTICLE_DIR = "article/";
    public static final String HEAD_IMG_DIR = "headImg/";
    public static final String FM_IMG_DIR = "fm/";
    public static final String DISK_SMALL_DIR = "disk/small/";
    public static final String DISK_BigL_DIR = "disk/big/";




    public static double convertToMB(long bytes) {
        double mb = (double) bytes / (1024 * 1024);
        return Math.round(mb * 100) / 100.0; // 保留两位小数
    }


    public static String builderCoverDir(Long userId){
        String date = LocalDate.now().toString();
        return userId.toString().concat("/").concat(COVER_DIR).concat(date).concat("/");
    }
    public static String builderArticleImgDir(Long userId){
        String date = LocalDate.now().toString();
        return userId.toString().concat("/").concat(ARTICLE_DIR).concat(date).concat("/");
    }
    public static String builderUserHeadImgDir(Long userId){
        String date = LocalDate.now().toString();
        return userId.toString().concat("/").concat(HEAD_IMG_DIR).concat(date).concat("/");
    }
    public static String builderUserFmImgDir(Long userId){
        String date = LocalDate.now().toString();
        return userId.toString().concat("/").concat(FM_IMG_DIR).concat(date).concat("/");
    }
    public static String builderDiskSmallFileDir(Long userId){
        String date = LocalDate.now().toString();
        return userId.toString().concat("/").concat(DISK_SMALL_DIR).concat(date).concat("/");
    }
    public static String builderDiskBigFileDir(Long userId,String md5){
        return userId.toString().concat("/").concat(DISK_BigL_DIR).concat(md5ConvertDir(md5)).concat("/").concat(md5).concat("/").concat("file").concat("/");
    }
    public static String builderDiskBigFileCutDir(Long userId,String md5){
        return userId.toString().concat("/").concat(DISK_BigL_DIR).concat(md5ConvertDir(md5)).concat("/").concat(md5).concat("/").concat("chunk").concat("/");
    }
    private static String md5ConvertDir(String md5){
        String dir = md5.substring(0,2);
        return dir;
    }
    public static String getFileType(String fileName) {
        int i = fileName.lastIndexOf(".");
        if (i == -1) {
            throw new IllegalArgumentException("文件名格式不正确");
        }
        return fileName.substring(i);
    }
}
